* ---------------------------------------------------------------------------- *
* This file produces the robustness graph for the markups regression
* ---------------------------------------------------------------------------- *

use "${replicated_data}\Robustness_Data.dta", clear

* Groups of controls
gen constant=1 
global N constant
global B signatory ln_q_b_all age_buy_month cohort_b
global R  ln_q_sb_all sh_s_b sh_b_s age_buysel_month cohort_sb
global O  ln_p_inpf_sbjo ln_q_sbjo
global BR signatory ln_q_b_all age_buy_month cohort_b ln_q_sb_all sh_s_b sh_b_s age_buysel_month cohort_sb
global BO signatory ln_q_b_all age_buy_month cohort_b  ln_p_inpf_sbjo ln_q_sbjo
global BRO signatory ln_q_b_all age_buy_month cohort_b  ln_p_inpf_sbjo ln_q_sbjo ln_q_sb_all sh_s_b sh_b_s age_buysel_month cohort_sb
global RO ln_q_sb_all sh_s_b sh_b_s age_buysel_month cohort_sb  ln_p_inpf_sbjo ln_q_sbjo

clear
cap program drop specchart
program specchart
syntax varlist, [replace] spec(string)
	* save current data
	tempfile temp
	save "`temp'",replace
	*dataset to store estimates
	if "`replace'"!=""{
			clear
			gen beta=.
			gen se=.
			gen spec_id=.
			gen u95=.
			gen u90=.
			gen l95=.
			gen l90=.
			gen NN=.
			save   "${replicated_data}\estimates_Mu.dta",replace
	}	
	else{
		* load dataset
		use  "${replicated_data}\estimates_Mu.dta",clear
	}
	* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta =_b[`varlist'] if  spec_id==`obs'
	replace se=_se[`varlist']   if  spec_id==`obs'
	replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	replace NN=e(N)  if  spec_id==`obs'
	
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 		     if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
		save  "${replicated_data}/estimates_Mu.dta",replace
	* restore dataset
	use `temp',clear
end



clear
cap program drop specchartdest
program specchartdest
syntax varlist, [replace] spec(string)
	* save current data
	tempfile temp
	save "`temp'",replace
	*dataset to store estimates
	if "`replace'"!=""{
			clear
			gen beta=.
			gen se=.
			gen spec_id=.
			gen u95=.
			gen u90=.
			gen l95=.
			gen l90=.
			gen NN=.
			save   "${replicated_data}\estimates_Mu_Clustered_dest.dta",replace
	}	
	else{
		* load dataset
		use  "${replicated_data}\estimates_Mu_Clustered_dest.dta",clear
	}
	* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta =_b[`varlist'] if  spec_id==`obs'
	replace se=_se[`varlist']   if  spec_id==`obs'
	replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	replace NN=e(N)  if  spec_id==`obs'
	
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 		     if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
		save  "${replicated_data}/estimates_Mu_Clustered_dest.dta",replace
	* restore dataset
	use `temp',clear
end


clear 
clear mata
clear matrix
set maxvar 120000 
use "${replicated_data}\Robustness_Data.dta"

* Main spec:
reghdfe ln_mu_sbjo   S_sourcing $BRO  if sample2==1, ///
abs(top_dest_b gr_sjy) vce (cluster b_id)
specchart S_sourcing, spec(main sjy dest sample2 BRO) replace


* Alternative Samples // baseline = 2
local S 2
foreach s of local S{

use "${replicated_data}\Robustness_Data.dta" , clear

capture drop constant
gen constant=1

clonevar sourcing=S_sourcing   // Standardized variable only defined for buyers in sample

* No fixed effects, no controls
qui: reghdfe ln_mu_sbjo   sourcing   ///
if sample`s'==1, ///
abs(constant) vce (cluster b_id)
specchart sourcing, spec( sample`s' N) 

* Controls - B, R, O...
local CTRL N B R O BR BO RO BRO 
foreach c of local CTRL {

* Only destination fixed effects
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(top_dest_b) vce (cluster b_id)	
specchart sourcing, spec(dest sample`s' `c') 



* Destination fixed effects and variations of all other fixed Effects (time, product, seller)
local FES y q m j s jy jq jm sy sq sm sj sjy sjq sjm 
foreach f of local FES{ 
	
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' top_dest_b) vce (cluster b_id)	
specchart sourcing, spec(`f' dest sample`s' `c') 

di "sam `s' control `c' FEs `f'"

}

* Destination variations and variations of all fixed effects
local FES  dj djy djq djm dy dm dq 
foreach f of local FES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' ) vce (cluster b_id)	
specchart sourcing, spec( `f'  sample`s' `c') 
di "sam `s' control `c' FEs `f'"

local GES s sj sy sjy 
foreach n of local GES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' gr_`n' ) vce (cluster b_id)	
specchart sourcing, spec( `f' `n' dest sample`s' `c') 

di "sam `s' control `c' FEs `f' and FEs `n'"
}
}

* Additive (version: upper tier is destination)
local FES ds dsj 
foreach f of local FES{ 
local GES y jy jq jm  
foreach n of local GES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' gr_`n' top_dest_b) vce (cluster b_id)	
specchart sourcing, spec( `f' `n' dest sample`s' `c') 

di "sam `s' control `c' FEs `f' and FEs `n'"
}
}

* Additive - two-way (version: upper tier is seller-time combinations, always with destination FEs)
local FES sy sq sm  
foreach f of local FES{ 
local GES j sj  
foreach n of local GES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' gr_`n' top_dest_b) vce (cluster b_id)	
specchart sourcing, spec( `f' `n' dest sample`s' `c') 

di "sam `s' control `c' FEs `f' and FEs `n'"
}
}

}

}



clear 
clear mata
clear matrix
set maxvar 120000 
use "${replicated_data}\Robustness_Data.dta"

* Main spec:
reghdfe ln_mu_sbjo   S_sourcing $BRO  if sample2==1, ///
abs(top_dest_b gr_sjy) vce (cluster top_dest_b)
specchartdest S_sourcing, spec(main sjy dest sample2 BRO) replace


* Alternative Samples // baseline = 2
local S 2
foreach s of local S{

use "${replicated_data}\Robustness_Data.dta" , clear

capture drop constant
gen constant=1

clonevar sourcing=S_sourcing   // Standardized variable only defined for buyers in sample

* No fixed effects, no controls
qui: reghdfe ln_mu_sbjo   sourcing   ///
if sample`s'==1, ///
abs(constant) vce (cluster b_id)
specchartdest sourcing, spec( sample`s' top_dest_b) 

* Controls - B, R, O...
local CTRL N B R O BR BO RO BRO 
foreach c of local CTRL {

* Only destination fixed effects
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(top_dest_b) vce (cluster top_dest_b)	
specchartdest sourcing, spec(dest sample`s' `c') 



* Destination fixed effects and variations of all other fixed Effects (time, product, seller)
local FES y q m j s jy jq jm sy sq sm sj sjy sjq sjm 
foreach f of local FES{ 
	
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' top_dest_b) vce (cluster top_dest_b)	
specchartdest sourcing, spec(`f' dest sample`s' `c') 

di "sam `s' control `c' FEs `f'"

}

* Destination variations and variations of all fixed effects
local FES  dj djy djq djm dy dm dq 
foreach f of local FES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' ) vce (cluster top_dest_b)	
specchartdest sourcing, spec( `f'  sample`s' `c') 
di "sam `s' control `c' FEs `f'"

local GES s sj sy sjy 
foreach n of local GES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' gr_`n' ) vce (cluster top_dest_b)	
specchartdest sourcing, spec( `f' `n' dest sample`s' `c') 

di "sam `s' control `c' FEs `f' and FEs `n'"
}
}

* Additive (version: upper tier is destination)
local FES ds dsj 
foreach f of local FES{ 
local GES y jy jq jm  
foreach n of local GES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' gr_`n' top_dest_b) vce (cluster top_dest_b)	
specchartdest sourcing, spec( `f' `n' dest sample`s' `c') 

di "sam `s' control `c' FEs `f' and FEs `n'"
}
}

* Additive - two-way (version: upper tier is seller-time combinations, always with destination FEs)
local FES sy sq sm  
foreach f of local FES{ 
local GES j sj  
foreach n of local GES{ 
qui: reghdfe ln_mu_sbjo   sourcing  $`c' ///
if sample`s'==1, ///
abs(gr_`f' gr_`n' top_dest_b) vce (cluster top_dest_b)	
specchartdest sourcing, spec( `f' `n' dest sample`s' `c') 

di "sam `s' control `c' FEs `f' and FEs `n'"
}
}

}

}
   
* Create graph
use "${replicated_data}\estimates_Mu.dta", clear

preserve 
use "${replicated_data}\estimates_Mu_Clustered_dest.dta", clear 
keep spec_id u95 u90 l95 l90 se beta
rename beta beta_control
duplicates drop
sort spec_id 
save "${replicated_data}\estimates_Mu_Clustered_destS.dta", replace
restore 

keep if sample2==1

duplicates drop
replace B=1 if (BR==1 | BO==1 | BRO==1 ) 
replace R=1 if (BR==1 | RO==1 | BRO==1 ) 
replace O=1 if (BO==1 | RO==1 | BRO==1 ) 
drop  BR BO RO BRO 
replace dest=. if (dj==1 | djy==1 | djq==1 | djm==1 | dy==1 | dm==1 | dq==1 | ds==1 | dsj==1 )

list if beta==0 & se==0, noobs 
* None; all runs completed
drop if beta==0 & se==0

duplicates drop main  dest B* R* O*  y* q*  m* j* s* d* N* sample*, force
/* sort estimates by coefficient size, uncomment to activate sort by category */

replace l90=. if se==0
replace l95=. if se==0
replace u90=. if se==0
replace u95=. if se==0
replace se=. if se==0

sort spec_id
merge 1:1 spec_id using "${replicated_data}\estimates_Mu_Clustered_destS.dta", update

count if beta!=beta_control

gen clust=(_merge==4)
replace clust=. if clust!=1

* 10% significance
capture drop sig
gen sig=abs(beta/se)
replace sig=sig>1.645
replace sig=. if sig!=1

sort beta
* rank
capture drop rank
gen rank=_n
* gen indicators and scatters
	local scoff=" "
	local scon=" "
	local ind=-0.02
	foreach var in dest y q m j s jy jq jm sjy sjq sjm sy sq sm sj dj djy dy dm dq djq djm ds dsj {
	   cap gen i_`var'=`ind'
	   local ind=`ind'-0.005
	   local scoff="`scoff' (scatter i_`var' rank,msize(vtiny) mcolor(gs13))" 
	   local scon="`scon' (scatter i_`var' rank if `var'==1,msize(tiny) mcolor(black))" 
	}
	local ind=`ind'-0.008
	foreach var in B R O  {
	   cap gen i_`var'=`ind'
	   local ind=`ind'-0.005
	   local scoff="`scoff' (scatter i_`var' rank,msize(vtiny) mcolor(gs13))" 
	   local scon="`scon' (scatter i_`var' rank if `var'==1,msize(tiny) mcolor(black))" 
	}

capture drop i_sig
gen i_sig = -0.17

capture drop i_clust 
gen i_clust = -0.175

sum beta if main==1
sum beta

* plot
tw  (scatter beta rank if main==1,  msymbol(x) msize(vlarge) mcolor(gs8) ) ///  main spec 
    (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) lwidth(none) barwidth(0.2)) /// 95% CI
	(scatter beta rank, mcolor(black) msymbol(o) msize(small)) ///  point estimates
	(scatter beta rank if main==1, msymbol(x) msize(vlarge) mcolor(gs8) ) ///  main spec 
	`scoff' `scon' /// indicators for spec
	(scatter i_B rank if main==1,msymbol(x) msize(vlarge) mcolor(gs8))  ///
	(scatter i_R rank if main==1,msymbol(x) msize(vlarge) mcolor(gs8)) ///
	(scatter i_O rank if main==1,msymbol(x) msize(vlarge) mcolor(gs8)) ///
	(scatter i_sjy rank if main==1,msymbol(x) msize(vlarge) mcolor(gs8))  ///
	(scatter i_dest rank if main==1,msymbol(x) msize(vlarge) mcolor(gs8))  ///
    (scatter i_sig rank if sig!=.,msize(tiny) mcolor(black)) /// 
	(scatter i_clust rank if clust!=.,msize(tiny) mcolor(black)) /// 
   ,legend (order(1 "Main specification" 3 "Point estimate" 2 "95% CI") region(lcolor(white)) ///
	pos(12) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") ///
   text(-0.183  261 "522 estimated coefficients; 98% of these different from zero at 10%", place(c) size(tiny)) ///
   	yline(0.0099551,  lpattern(-) lcolor(grey) lwidth(vvthin) ) ///
   yline(0.0481204, lpattern(-) lcolor(grey) lwidth(vvthin) ) ///
   yscale() xscale(noline) ylab("",noticks nogrid angle(horizontal)) xlab("", noticks)  ///
   yline(0,  lpattern(_) lcolor(grey) lwidth(vthin) ) ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white))


* label relevant values on y axis
gr_edit .yaxis1.add_ticks 0 `"0"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks  0.0099551  `"0.0099"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) ) // min
gr_edit .yaxis1.add_ticks 0.0481204 `"0.0481"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) ) // max

* now add stuff to the y axis   
gr_edit .yaxis1.add_ticks -0.015 `"FEs        "', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.02 `"d"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.025 `"y"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.03 `"q"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.035 `"m"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.04 `"j"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.045 `"s"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.05 `"jy"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.055 `"jq"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.06 `"jm"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.065 `"sjy"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.07 `"sjq"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.075 `"sjm"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.08 `"sy"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.085 `"sq"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.09 `"sm"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.095 `"sj"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.1 `"dj"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.105 `"djy"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.11 `"dy"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.115 `"dm"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.12 `"dq"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.125 `"djq"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.13 `"djm"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.135 `"ds"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.14 `"dsj"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.145 `"Controls      "', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.151 `"Buyer"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.156 `"Relationship"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.161 `"Order"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.17 `"Significant (10%)"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks -0.175 `"Cluster: Dest"', custom tickset(major) editstyle(tickstyle(textstyle(size(tiny))) )
gr_edit .yaxis1.add_ticks 0.1 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks 0.095 `"Markup"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

graph export "${figures}\Figure3.pdf", as(pdf) name("Graph") replace